package br.ufpe.cin.rdfilter.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import br.ufpe.cin.rdfilter.feedback.Feedback;
import br.ufpe.cin.rdfilter.model.Manager;
import br.ufpe.cin.rdfilter.model.Query;
import br.ufpe.cin.rdfilter.model.Source;
import br.ufpe.cin.rdfilter.model.User;
import br.ufpe.cin.rdfilter.query.QueryManager;
import br.ufpe.cin.rdfilter.relevance.Relevance;
import br.ufpe.cin.rdfilter.util.FeedbackInference;
import br.ufpe.cin.rdfilter.util.LoadFeedback;
import br.ufpe.cin.rdfilter.util.LoadQuery;

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.RDFNode;

@SuppressWarnings("serial")
public class FramePrincipal extends JFrame {

	public static String QUERY = "PREFIX id:  <http://dblp.rkbexplorer.com/id/>"
			+ " PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
			+ " PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
			+ " PREFIX akt:  <http://www.aktors.org/ontology/portal#>"
			+ " PREFIX owl:  <http://www.w3.org/2002/07/owl#>"
			+ " PREFIX foaf:    <http://xmlns.com/foaf/0.1/>"
			+ " SELECT ?nomeArtigo"
			+ " WHERE {"
			+ " ?artigo akt:has-title ?nomeArtigo ."
			+ " ?artigo akt:has-author ?nomeAutor ."
			+ " ?nomeAutor akt:full-name \"Patrick C. Fischer\" ." + " }";

	public static String QUERY02 = " PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
			+ "PREFIX akt: <http://www.aktors.org/ontology/portal#>"
			+ "PREFIX akt-date: <http://www.aktors.org/ontology/date#>"
			+ "SELECT DISTINCT ?articleTitle WHERE {"
			+ "  ?article akt:has-title ?articleTitle ."
			+ " ?article akt:has-date ?date ."
			+ "  FILTER(regex(str(?date),\"1990\"))}";
/*
	public static String[] ENDPOINTS = { "http://oai.rkbexplorer.com/sparql/",
			"http://dotac.rkbexplorer.com/sparql/",
			"http://kisti.rkbexplorer.com/sparql/",
			"http://acm.rkbexplorer.com/sparql/",
			"http://nsf.rkbexplorer.com/sparql/",
			"http://eprints.rkbexplorer.com/sparql/",
			"http://citeseer.rkbexplorer.com/sparql/",
			"http://rae2001.rkbexplorer.com/sparql/",
			"http://unlocode.rkbexplorer.com/sparql/",
			"http://risks.rkbexplorer.com/sparql/",
			"http://southampton.rkbexplorer.com/sparql/",
			"http://irit.rkbexplorer.com/sparql/",
			"http://os.rkbexplorer.com/sparql/",
			"http://era.rkbexplorer.com/sparql/",
			"http://ieee.rkbexplorer.com/sparql/",
			"http://newcastle.rkbexplorer.com/sparql/",
			"http://jisc.rkbexplorer.com/sparql/",
			"http://resex.rkbexplorer.com/sparql/",
			"http://laas.rkbexplorer.com/sparql/",
			"http://courseware.rkbexplorer.com/sparql/",
			"http://ibm.rkbexplorer.com/sparql/",
			"http://wiki.rkbexplorer.com/sparql/",
			"http://pisa.rkbexplorer.com/sparql/",
			"http://budapest.rkbexplorer.com/sparql/",
			"http://roma.rkbexplorer.com/sparql/",
			"http://deploy.rkbexplorer.com/sparql/",
			"http://eurecom.rkbexplorer.com/sparql/",
			"http://ulm.rkbexplorer.com/sparql/",
			"http://ft.rkbexplorer.com/sparql/",
			"http://deepblue.rkbexplorer.com/sparql/",
			"http://curriculum.rkbexplorer.com/sparql/",
			"http://darmstadt.rkbexplorer.com/sparql/" };
*/
	
	private static String[] ENDPOINTS = {
		"http://localhost:2020/sparql/ACM", 
        "http://localhost:2020/sparql/Budapest", 
        "http://localhost:2020/sparql/CiteSeer", 
        "http://localhost:2020/sparql/Courseware", 
        "http://localhost:2020/sparql/Curriculum", 
        "http://localhost:2020/sparql/Darmstadt", 
        "http://localhost:2020/sparql/DeepBlue", 
        "http://localhost:2020/sparql/DEPLOY", 
        "http://localhost:2020/sparql/dotAC", 
        "http://localhost:2020/sparql/ERA", 
        "http://localhost:2020/sparql/Eurecom", 
        "http://localhost:2020/sparql/FT", 
        "http://localhost:2020/sparql/IBM", 
        "http://localhost:2020/sparql/IEEE", 
        "http://localhost:2020/sparql/IRIT", 
        "http://localhost:2020/sparql/JISC", 
        "http://localhost:2020/sparql/KIST", 
        "http://localhost:2020/sparql/LAAS", 
        "http://localhost:2020/sparql/Newcastle", 
        "http://localhost:2020/sparql/NSF", 
        //"http://localhost:2020/sparql/OAI", 
        "http://localhost:2020/sparql/OS", 
        "http://localhost:2020/sparql/Pisa", 
        "http://localhost:2020/sparql/RAE2001", 
        "http://localhost:2020/sparql/RESEX", 
        "http://localhost:2020/sparql/RISKS", 
        "http://localhost:2020/sparql/Roma", 
        "http://localhost:2020/sparql/Southampton", 
        "http://localhost:2020/sparql/Ulm", 
        "http://localhost:2020/sparql/UNLOCODE", 
        "http://localhost:2020/sparql/Wiki"
		
	}; 
	
	private javax.swing.JTextArea consultaTextArea;
	private javax.swing.JLabel endpointLabel;
	private javax.swing.JTextField endpointTextField;
	private javax.swing.JMenu menuFile;
	private javax.swing.JMenuItem menuCreateUser;
	private JMenuItem menuCreateSource;
	private JMenuItem menuCreateQuery;
	private javax.swing.JMenu menuRelevance;
	private javax.swing.JMenuItem menuItemCalculate;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JMenuBar menuBar;
	private javax.swing.JButton searchButton;
	private javax.swing.JLabel idConsultaLabel;
	private javax.swing.JTextField idConsultaTextField;
	private javax.swing.JLabel idUserLabel;
	private javax.swing.JTextField idUserTextField;
	private List<String> dataQuery;

	// private JComboBox combo;

	public FramePrincipal() {

		super("Consulta em Datasets");
		initComponents();
		setVisible(true);

	}

	private void initComponents() {
		jScrollPane1 = new javax.swing.JScrollPane();
		consultaTextArea = new javax.swing.JTextArea();
		endpointLabel = new javax.swing.JLabel();
		endpointTextField = new javax.swing.JTextField();
		searchButton = new javax.swing.JButton();
		menuBar = new javax.swing.JMenuBar();
		idConsultaTextField = new javax.swing.JTextField();
		idConsultaLabel = new javax.swing.JLabel();
		idUserTextField = new javax.swing.JTextField();
		idUserLabel = new javax.swing.JLabel();
		menuFile = new javax.swing.JMenu();
		menuCreateUser = new javax.swing.JMenuItem();
		menuCreateSource = new JMenuItem();
		menuCreateQuery = new JMenuItem();
		menuRelevance = new javax.swing.JMenu();
		menuItemCalculate = new JMenuItem();
		/* teste */
		/*
		 * Manager mm = new Manager(); List<User> teste2 = mm.getUsers();
		 * List<String> list = new ArrayList<String>(); for(int
		 * i=0;i<teste2.size();i++) list.add(teste2.get(i).getName());
		 * 
		 * String[] teste = list.toArray(new String[list.size()]); combo = new
		 * JComboBox(teste);
		 */

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		consultaTextArea.setColumns(20);
		consultaTextArea.setRows(5);
		consultaTextArea.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Consulta"));
		jScrollPane1.setViewportView(consultaTextArea);

		endpointLabel.setText("Endpoint Address");

		idConsultaLabel.setText("ID Consulta");

		idUserLabel.setText("User");
		searchButton.setText("Search");
		searchButton.setActionCommand("Submit");

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				QueryManager qm = new QueryManager();

				qm.executeQuery(endpointTextField.getText(),
						consultaTextArea.getText(),
						idConsultaTextField.getText());

				// codigo para usar o banco (temporario)

				// Manager m = new Manager();
				// m.addUser(idUserTextField.getText());
				// m.addQuery(idConsultaTextField.getText(),
				// idConsultaTextField.getText(), consultaTextArea.getText());
				// m.addSource("DBLP",endpointTextField.getText());

				// m.addFeedback();
				// m.stop();
				// fim do codigo			
				
				new FeedBackGui(qm.getColumn(), idConsultaTextField.getText(),
						idUserTextField.getText(), qm.getDataQuery());

			}
		});

		menuFile.setText("File");
		menuCreateUser.setText("Create User");
		menuCreateSource.setText("Create Source");
		menuCreateQuery.setText("Create Query");

		menuRelevance.setText("Relevance");
		menuItemCalculate.setText("Calculate");

		menuBar.add(menuFile);
		menuBar.add(menuRelevance);

		menuFile.add(menuCreateUser);
		menuFile.add(menuCreateSource);
		menuFile.add(menuCreateQuery);

		menuRelevance.add(menuItemCalculate);

		menuCreateUser.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				menuCreateUserActionPerformed(evt);
			}
		});

		menuCreateSource.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				menuCreateSourceActionPerformed(evt);
			}
		});

		menuCreateQuery.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				menuCreateQueryActionPerformed(evt);
			}
		});

		menuItemCalculate
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						menuItemCalculateActionPerformed(evt);
					}
				});

		setJMenuBar(menuBar);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addContainerGap()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addComponent(
																						jScrollPane1,
																						javax.swing.GroupLayout.Alignment.TRAILING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														endpointLabel)
																												.addComponent(
																														idUserLabel)
																												.addComponent(
																														idConsultaLabel)
																								// .addComponent(idConsultaLabel)
																								)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														endpointTextField)
																												// .addComponent(combo)
																												.addComponent(
																														idUserTextField)
																												.addGroup(
																														layout.createSequentialGroup()
																																.addComponent(
																																		idConsultaTextField,
																																		javax.swing.GroupLayout.PREFERRED_SIZE,
																																		249,
																																		javax.swing.GroupLayout.PREFERRED_SIZE)
																																.addGap(0,
																																		0,
																																		Short.MAX_VALUE))))))
												.addGroup(
														layout.createSequentialGroup()
																.addGap(282,
																		282,
																		282)
																.addComponent(
																		searchButton,
																		javax.swing.GroupLayout.PREFERRED_SIZE,
																		94,
																		javax.swing.GroupLayout.PREFERRED_SIZE)
																.addGap(0,
																		244,
																		Short.MAX_VALUE)))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														idConsultaTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(idConsultaLabel))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)

								// .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								// .addComponent(combo,
								// javax.swing.GroupLayout.PREFERRED_SIZE,
								// javax.swing.GroupLayout.DEFAULT_SIZE,
								// javax.swing.GroupLayout.PREFERRED_SIZE)
								// .addComponent(idUserLabel))
								// .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)

								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														idUserTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(idUserLabel))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)

								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(endpointLabel)
												.addComponent(
														endpointTextField,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addGap(18, 18, 18)
								.addComponent(jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										271,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGap(18, 18, 18)
								.addComponent(searchButton,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										24, Short.MAX_VALUE).addContainerGap()));

		pack();

	}

	private void menuCreateUserActionPerformed(ActionEvent evt) {
		new FrameUser().setVisible(true);
	}

	private void menuCreateSourceActionPerformed(ActionEvent evt) {
		new FrameSource().setVisible(true);
	}

	private void menuCreateQueryActionPerformed(ActionEvent evt) {
		new FrameQuery().setVisible(true);
	}

	private void menuItemCalculateActionPerformed(ActionEvent evt) {
		// Relevance rel = new
		// Relevance(endpointTextField.getText(),consultaTextArea.getText());


//					Relevance rel = new Relevance();	
		long tempoInicial = System.currentTimeMillis();  
		
		Manager m = new Manager();
		List<Query> q = m.getSomeQueries();
		String[] temp = new String[13];
		for (int i=0;i<5;i++){
			temp[i]=q.get(i).getStatement();
		}	
		for (int i = 0; i < ENDPOINTS.length; i++) {
			for(int j=0;j<5;j++){
				Relevance rel = new Relevance(ENDPOINTS[i], q.get(j).getStatement(), q.get(j).getLabel(), "notes.txt");
			}
			
			// rel.print();
		}

		long tempoFinal = System.currentTimeMillis();  
		System.out.println("Tempo de execução: " + (tempoFinal - tempoInicial) );
		

		JOptionPane.showMessageDialog(null,
				"Relevance Analysis finished with success!");
	}

	public static void main(String[] args) {
		new FramePrincipal();

	}

}
